<cfcomponent extends="Controller">
	<cffunction name="init">
		<cfscript>
			THIS.navigation = [
				{	"home" 				= 0	},
				{ 	"about" 			= 0	},
				{ 	"lessons" 			= 0	},
				{ 	"calendar" 			= 0	},
				{	"testimonials" 		= 0	},
				{	"contact" 			= 0 }
			];
			
			set(navigation = THIS.navigation);
			filters(through="setNavigation");
			filters(through="checkLogin");
		</cfscript>
	</cffunction>
	
	<cffunction name="setNavigation">
		<cfscript>
			THIS.navigation = get("navigation");
			
			for(intRow = 1; intRow LTE ArrayLen(THIS.navigation); intRow++) {
				if(StructKeyExists(THIS.navigation[intRow], PARAMS.action))
					THIS.navigation[intRow][PARAMS.action] = [];
			}

			set(navigation = THIS.navigation);
			navigation = get("navigation");
		</cfscript>
	</cffunction>

	<cffunction name="checkLogin">
		<cfscript>
			if(NOT IsDefined("SESSION.user")) {
				redirectTo(
					controller = "main",
					action = "login",
					error = "You have been signed out due to inactivity, please sign in again!"
				);
			}
		</cfscript>
	</cffunction>

	<cffunction name="employees">
		<cfset employees = model("employee").findAll(include="employeeStudents(student)", order="last_name ASC, id ASC")>
	</cffunction>

	<cffunction name="parents">
		<cfset parents = model("parent").findAll(include="parentStudents(student)", order="last_name ASC, id ASC")>
	</cffunction>

	<cffunction name="students">
		<cfset students = model("student").findAll(order="last_name ASC, id ASC")>
	</cffunction>

	<cffunction name="goals">
		<cfset goals = model("goal").findAll()>
	</cffunction>

	<cffunction name="addItem">
		<cfscript>
			item = model(PARAMS.model).new(PARAMS[PARAMS.model]);
			item['updated_by'] = SESSION.user.id;
			flashMsg = "Information for <strong>#item.first_name# #item.last_name#</strong> has been successfully created!";
			item.save();
		</cfscript>

		<cfif(PARAMS.model IS "student">
			<!---
			<cfquery name="qryInsertGoal" datasource="#APPLICATION.wheels.datasource#">
			</cfquery>
			--->
		</cfif>

		<cfset redirectTo(
			controller = "admin",
			action = pluralize(PARAMS.model),
			success = flashMsg
		)>
	</cffunction>

	<cffunction name="updateItem">
		<cfscript>
			var mainModel = model(PARAMS.model).findByKey(PARAMS.key);
			var subModel = (PARAMS.model IS "student") ? model(PARAMS.subModel) : model("#PARAMS.model##PARAMS.subModel#");
			var subIDs = (PARAMS.model IS "student") ? "" : ListToArray(PARAMS["#PARAMS.model##pluralize(PARAMS.subModel)#"]["#PARAMS.subModel#_id"]);
			var modelProperties = PARAMS[PARAMS.model];
			var subModelProperties = {
					"#PARAMS.model#_id" = PARAMS.key,
					'updated_by' = SESSION.user.id,
					'updated_date' = Now()
				};

			if(PARAMS.model IS "student") {
				for(id IN PARAMS.goal.goal) {
					subModelProperties["goal"] = PARAMS.goal.goal[id];
					subModel.findByKey(id).update(subModelProperties);
				}

				mainModel.update(modelProperties);
			} else {
				subModel.deleteAll(where="#PARAMS.model#_id = #PARAMS.key#");

				for(subID IN subIDs) {
					subModelProperties["#PARAMS.subModel#_id"] = subID;
					subModel.create(subModelProperties);
				}

				mainModel.update(modelProperties);
			}

			flashMsg = "Information for <strong>#modelProperties.first_name# #modelProperties.last_name#</strong> has been successfully updated!";
		
			redirectTo(
				controller = "admin",
				action = pluralize(PARAMS.model),
				success = flashMsg
			);
		</cfscript>
	</cffunction>

	<cffunction name="deleteItem">
		<cfscript>
			var mainModel = model(PARAMS.model);
			var item = mainModel.findAll(where="id = #PARAMS.key#");
			var flashMsg = "Information for <strong>#item.first_name# #item.last_name#</strong> has been successfully deleted!";
			
			mainModel.deleteAll(where="id = #PARAMS.key#");

			if(PARAMS.model IS "student") {
				model("goal").deleteAll(where="student_id = #PARAMS.key#");
			}

			redirectTo(
				controller = "admin",
				action = pluralize(PARAMS.model),
				success = flashMsg
			);
		</cfscript>
	</cffunction>

	<cffunction name="deleteSubItem">
		<cfscript>
			var mainModel = model(PARAMS.model);
			var subModel = model("#PARAMS.model##PARAMS.subModel#");
			var subItem = subModel.findOne(where="id=#PARAMS.key#", include="#PARAMS.subModel#");
			var item = mainModel.findByKey(subItem["#PARAMS.model#_id"]);
			var flashMsg = "Association of <strong>#subItem[PARAMS.subModel].first_name# #subItem[PARAMS.subModel].last_name#</strong> for ";
				flashMsg &= "<strong>#item.first_name# #item.last_name#</strong> has been successfully deleted!";

			subItem.delete();

			redirectTo(
				controller = "admin",
				action = pluralize(PARAMS.model),
				success = flashMsg
			);
		</cfscript>
	</cffunction>

	<cffunction name="updateForm">
		<cfscript>
			var association = "#PARAMS.model##pluralize(PARAMS.subModel)#(#PARAMS.subModel#)";
			PARAMS.routeType = (PARAMS.key) ? "updateItem" : "addItem";

			if(PARAMS.model IS "student") {
				item = {
					"model" = (PARAMS.key) ? 
								model(PARAMS.model).findAll(where="id=#PARAMS.key#",include="#PARAMS.subModel#",returnAs="query") : 
								model(PARAMS.model).new(include="#PARAMS.subModel#", returnAs="query")
					};
			} else {
				item = {
					"model" = (PARAMS.key) ? 
								model(PARAMS.model).findAll(where="id=#PARAMS.key#",include="#association#",returnAs="query") : 
								model(PARAMS.model).new(include="#PARAMS.subModel#", returnAs="query")
				};
			}

			item["columns"] = {};

			switch(PARAMS.model) {
				case "employee":
					item.columns = {
						"list" = "first_name,last_name,address,city,state,zip,email",
						"first_name" = { "maxlength" = 50, "validate"="validate[required]" },
						"last_name" = { "maxlength" = 50, "validate"="validate[required]" },
						"address" = { "maxlength" = 50, "validate"="validate[required]" },
						"state" = { "maxlength" = 2, "validate"="validate[required]" },
						"city" = { "maxlength" = 50, "validate"="validate[required]" },
						"zip" = { "maxlength" = 5, "validate"="validate[required] numeric" },
						"email" = { "maxlength" = 50, "validate"="validate[required,custom[email]]" }
					};
					break;

				case "parent":
					item.columns = {
						"list" = "first_name,last_name,address,city,state,zip,email",
						"first_name" = { "maxlength" = 50, "validate"="validate[required]" },
						"last_name" = { "maxlength" = 50, "validate"="validate[required]" },
						"address" = { "maxlength" = 50, "validate"="validate[required]" },
						"state" = { "maxlength" = 2, "validate"="validate[required]" },
						"city" = { "maxlength" = 50, "validate"="validate[required]" },
						"zip" = { "maxlength" = 5, "validate"="validate[required] numeric" },
						"email" = { "maxlength" = 50, "validate"="validate[required,custom[email]]" }
					};
					break;

				case "student":
					item.columns = {
						"list" = "first_name,last_name,address,city,state,zip,email,sex",
						"first_name" = { "maxlength" = 50, "validate"="validate[required]" },
						"last_name" = { "maxlength" = 50, "validate"="validate[required]" },
						"address" = { "maxlength" = 50, "validate"="validate[required]" },
						"state" = { "maxlength" = 2, "validate"="validate[required]" },
						"city" = { "maxlength" = 50, "validate"="validate[required]" },
						"zip" = { "maxlength" = 5, "validate"="validate[required] numeric" },
						"email" = { "maxlength" = 50, "validate"="validate[required,custom[email]]" },
						"sex" = { "maxlength" = 0, "validate" = "validate[required]"}
					};
					break;
			}
		</cfscript>
	</cffunction>
</cfcomponent>
